const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        projectList: [],
        projectLoading: true,
        pages: 1
    },
    onLoad: function (options) {
        this.getProjectList()
    },
    getProjectList() {
        const that = this
        ajax.post({
            url: '/wx/getApplyList',
            data: {
                page: 1,
                limit: 10
            },
            success(res) {
                that.setData({
                    projectList: res.data.list
                })
            }
        })
    },
    toBuyDetails(e) {
        const buyId = e.currentTarget.dataset.buyid
        wx.navigateTo({
            url: `/pages/Mine/buyDetails/buyDetails?buyId=${buyId}`,
        })
    },

    onReachBottom () {
        (this.data.pages) ++;
        this.reachBottomBuy()
    },
    
    reachBottomBuy() {
        const that = this
        ajax.post({
            url: '/wx/getApplyList',
            data: {
                page: that.data.pages,
                limit: 10
            },
            success(res) {
                let projectList = res.data.list
                if (projectList.length === 0) {
                    that.setData({
                        projectLoading: true
                    })
                    return
                }
                
                if (that.data.pages === 1) {
                    that.setData({
                        projectList: projectList
                    })
                    return
                }

                if (that.data.pages !== 1) {
                    let newList = that.data.projectList.concat(projectList)
                    that.setData({
                        projectList: newList
                    })
                    return
                }
            }
        })
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.getProjectList()
        wx.stopPullDownRefresh()
    }
})